package com.iyang.jpa.main;

import com.iyang.jpa.pojo.Student;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.List;

/**
 * @Author: Mu_YI
 * @Date: 2019/7/21 13:31
 * @Version 1.0
 */
public class StudentMain {

    /**
     * 添加
     *  返回结果来看:  persist 没有返回   merge会返回一个对象
     *               persist 不能操作脱离对象   merge是操作脱离对象
     *              refresh 也不能操作脱离对象;如果是new对象的话,也会抛出异常  操作托管状态是可以的
     *  个人认为 : 添加使用 persist   更新使用 merge
     *  merge 执行的时候;是会在之前发送一次 select 的Sql语句
     */
    public static void main(String[] args) {
        /*updateStudent();*/
        /*selectStudent();*/
        deleteStudent();
    }

    /**
     * 添加
     */
    private static void addStudent(){
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("YangJpa");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        Student student = new Student();
        student.setName("Yang");
        entityManager.persist(student);
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }

    /**
     * 更新
     */
    private static void updateStudent(){
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("YangJpa");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        Student student = entityManager.find(Student.class, 1L);
        student.setName("Yang Update");
        entityManager.merge(student);
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }

    /**
     * 查询
     * Query
     */
    private static void selectStudent(){
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("YangJpa");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        List<Student> studentList = entityManager.createQuery("select s from Student s", Student.class).getResultList();
        System.out.println("查询出:" + studentList);
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }

    /**
     * 删除Student
     */
    private static void deleteStudent(){
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("YangJpa");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        Student student = entityManager.find(Student.class, 1L);
        entityManager.remove(student);
        transaction.commit();
        entityManager.close();
        entityManagerFactory.close();
    }

}
